<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 左侧系统介绍 -->
      <div class="intro-box">
        <div class="header-section">
          <h1 class="welcome-text">欢迎登录云盘管理系统</h1>
          <p class="system-intro">安全、高效的文件存储与共享平台</p>
        </div>

        <div class="features-grid">
          <div
              v-for="(feature, index) in features"
              :key="index"
              class="feature-card"
          >
            <el-icon :size="32" class="feature-icon">
              <component :is="feature.icon" />
            </el-icon>
            <div class="feature-content">
              <h3 class="feature-title">{{ feature.title }}</h3>
              <p class="feature-description">{{ feature.description }}</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="form-section">
        <div class="form-wrapper">
          <div class="form-header">
            <h2 class="form-title">账户登录</h2>
            <p class="form-subtitle">请使用您的账户凭证登录系统</p>
          </div>

          <el-form ref="formRef" :model="data.form" :rules="data.rules">
            <el-form-item prop="username">
              <el-input
                  v-model="data.form.username"
                  :prefix-icon="User"
                  placeholder="请输入账号"
                  size="large"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                  v-model="data.form.password"
                  :prefix-icon="Lock"
                  placeholder="请输入密码"
                  show-password
                  size="large"
              />
            </el-form-item>
          </el-form>

          <div class="form-actions">
            <el-button
                type="primary"
                size="large"
                class="login-btn"
                @click="login"
            >
              立即登录
            </el-button>
            <div class="secondary-actions">
              还没有账号？<a href="/register">立即注册</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { Lock, User } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { loginApi } from "@/utils/API/auth";

const router = useRouter();

// 特性数据
const features = [
  { icon: 'Files', title: '文件存储', description: '海量存储空间，安全可靠' },
  { icon: 'UploadFilled', title: '多端同步', description: '支持多设备实时同步，随时随地访问' },
  { icon: 'Unlock', title: '安全保障', description: '高级加密技术，保护您的隐私' },
  { icon: 'Share', title: '共享协作', description: '便捷的文件共享与团队协作功能' }
]

// 表单数据
const data = reactive({
  form: {
    username: '',
    password: ''
  },
  rules: {
    username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
  }
})

const formRef = ref()

// 登录方法
const login = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      loginApi(data.form)
          .then(res => {
            if (res.code === 200) {
              ElMessage.success("登录成功")
              localStorage.setItem('system-user', JSON.stringify(res.data))
              localStorage.setItem('expiration', Date.now())
              // TODO 跳转到系统主页
              router.push("/cloud-drive")
            } else {
              ElMessage.error(res.msg)
            }
          })
          .catch(error => {
            console.error(error)
            ElMessage.error("登录失败，请检查网络连接")
          })
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 2rem;
}

.login-box {
  display: flex;
  background: white;
  border-radius: 1.5rem;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);
  max-width: 1200px;
  width: 100%;
  min-height: 680px;
  overflow: hidden;
}

/* 左侧系统介绍 */
.intro-box {
  flex: 1;
  padding: 3rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  flex-direction: column;
}

.header-section {
  margin-bottom: auto;
}

.welcome-text {
  font-size: 2.2rem;
  line-height: 1.3;
  margin-bottom: 1rem;
}

.system-intro {
  font-size: 1.1rem;
  opacity: 0.9;
}

/* 特性网格 */
.features-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1.5rem;
  margin-top: 2rem;
}

.feature-card {
  display: flex;
  align-items: flex-start;
  padding: 1.5rem;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  transition: transform 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-3px);
}

.feature-icon {
  margin-right: 1rem;
  flex-shrink: 0;
}

.feature-content {
  flex: 1;
}

.feature-title {
  font-size: 1.1rem;
  margin-bottom: 0.5rem;
}

.feature-description {
  font-size: 0.95rem;
  opacity: 0.85;
  line-height: 1.5;
}

/* 右侧表单 */
.form-section {
  flex: 0 0 45%;
  padding: 4rem 3rem;
  display: flex;
  align-items: center;
}

.form-wrapper {
  width: 100%;
  max-width: 400px;
}

.form-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.form-title {
  font-size: 1.8rem;
  margin-bottom: 0.5rem;
}

.form-subtitle {
  color: #666;
  font-size: 0.95rem;
}

/* 表单元素 */
:deep(.el-input__wrapper) {
  padding: 0.8rem 1rem;
  border-radius: 10px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.login-btn {
  width: 100%;
  font-weight: 500;
  letter-spacing: 0.5px;
  height: 48px;
  transition: all 0.3s ease;
}

.form-actions {
  margin-top: 2rem;
}

.secondary-actions {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 1.5rem;
  gap: 1rem;
}

.link-text {
  color: #666;
  transition: color 0.3s;
  font-size: 0.95rem;
  text-decoration: none;
}

.link-text:hover {
  color: #409eff;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .login-box {
    flex-direction: column;
    min-height: auto;
  }

  .intro-box {
    padding: 2rem;
    border-radius: 1.5rem 1.5rem 0 0;
  }

  .form-section {
    padding: 3rem 2rem;
  }

  .features-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 576px) {
  .login-container {
    padding: 1rem;
  }

  .welcome-text {
    font-size: 1.8rem;
  }

  .form-section {
    padding: 2rem 1.5rem;
  }
}
</style>